#!/usr/bin/python
# -*- coding: UTF-8 -*-

from kpautos.engine.case import Case
from kptest.config.enum import DeviceType, HostType, DeviceUtility
from extension.utils import password_generator


class BMC_Redfish_08(Case):
    """
        Name:
            BMC_Redfish_08
        Label:
            检查机架资源信息
        PreCondition:
            1、BMC正常运行
        TestStep:
            1、发送redfish请求，查看机架资源信息
        ExpectedResult:
            A）Redfish可以正常显示机架资源信息
            B)名字、类型、厂商、型号、序列号、资产标签、定位灯状态信息能够正确显示
        Parameters:

        Remark:

    """

    # 测试执行前配置等准备操作
    def pre_test_case(self):
        self.device_dut = self.resource.get_device(device_type=DeviceType.Server, utility=DeviceUtility.DUT)
        self.os_host = self.device_dut.get_host(host_type=HostType.HostOS)
        self.redfish_api = self.device_dut.get_api(ns_name="Redfish")

        # 测试步骤
    def procedure(self):
        self.log_step(" 1、发送请求，检查机架资源集合")
        result = self.redfish_api.redfish_chassis_query_chassis_collection()
        self.logger.info(f"Redfish查询到的机架资源集合为：{result}")
        if result == None:
            raise Exception("Redfish检查机架资源集合失败")
        self.log_step(" 2、检查机架资源的详细信息")
        chassis_member = result['Members']
        for i in range(len(chassis_member)):
            chassis_id = chassis_member[i]['@odata.id'].split('/')[-1]
            chassis_info = self.redfish_api.redfish_chassis_query_chassis_id(chassis_id)
            if chassis_info == None:
                raise Exception(f"Redfish检查机架资源信息失败")
            self.log_debug(f"机架资源详细信息为: {chassis_info}")
            self.log_info(f"名字：{chassis_info['Name']}")
            self.log_info(f"类型：{chassis_info['ChassisType']}")
            self.log_info(f"厂商：{chassis_info['Manufacturer']}")
            self.log_info(f"型号：{chassis_info['Model']}")
            self.log_info(f"序列号：{chassis_info['SerialNumber']}")
            self.log_info(f"资产标签：{chassis_info['PartNumber']}")
            self.log_info(f"定位灯状态：{chassis_info['IndicatorLED']}")

    # 测试结束后恢复环境
    def post_test_case(self):
        pass
